﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SgAutoArchiveBrrDal.Linq
{
    public class T_CARDINFO_Dal
    {

        /// <summary>
        /// 添加一条登记
        /// </summary>
        /// <param name="en"></param>
        /// <returns></returns>
        public static int Add_TCARDINFO(T_CARDINFO en)
        {
            SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
            conn.T_CARDINFO.Add(en);
                conn.SaveChanges();
                return en.C_ID;
        }
        public static List<T_CARDINFO> Query_SP()
        {
            try
            {
                SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
                var query = conn.T_CARDINFO.Select(c => c);

                query = query.Where(c => c.C_I01 == 0 && c.C_I01!=2);

                return query.ToList();
            }
            catch(Exception ex ) { return null; }
        }
        public static List<T_CARDINFO> Query_SPDC()
        {
            try
            {
                SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
                var query = conn.T_CARDINFO.Select(c => c);

                query = query.Where(c => c.C_I01 == 0 && c.C_STAUTS == 1);

                return query.ToList();
            }
            catch { return null; }
        }
        public static int Modify_CardI01(int id, T_CARDINFO en)
        {
            SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
            if (conn.T_CARDINFO.Select(c => c).Where(c => c.C_CARDNO == en.C_CARDNO && c.C_ID!= en.C_ID).Count() > 0)
                return 0;
            else
            {
                var query = conn.T_CARDINFO.Select(c => c).Where(c => c.C_ID == id);
                foreach (var n in query)
                {
                    #region 绑定数据
                    n.C_I01 = en.C_I01;
                    n.C_STAUTS = en.C_STAUTS;
                    //n.C_V01 = en.C_V01;
                    //n.C_V02 = en.C_V02;
                    //n.C_CBMP = en.C_CBMP;
                    //n.C_PIMG = en.C_PIMG;
                    //n.c_PDF = en.c_PDF;
                    #endregion
                }
                conn.SaveChanges();
                return 0;
            }
        }
        public static int Modify_stauts(int id, T_CARDINFO en)
        {
            SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
            if (conn.T_CARDINFO.Select(c => c).Where(c => c.C_CARDNO == en.C_CARDNO && c.C_ID != en.C_ID).Count() > 0)
                return 0;
            else
            {
                var query = conn.T_CARDINFO.Select(c => c).Where(c => c.C_ID == id);
                foreach (var n in query)
                {
                    #region 绑定数据
                    //n.C_I01 = en.C_I01;
                    n.C_STAUTS = en.C_STAUTS;
                    //n.C_V01 = en.C_V01;
                    //n.C_V02 = en.C_V02;
                    //n.C_CBMP = en.C_CBMP;
                    //n.C_PIMG = en.C_PIMG;
                    //n.c_PDF = en.c_PDF;
                    #endregion
                }
                conn.SaveChanges();
                return 0;
            }
        }
        public static int Modify_CardV01(int id, T_CARDINFO en)
        {
            SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
            if (conn.T_CARDINFO.Select(c => c).Where(c => c.C_CARDNO == en.C_CARDNO && c.C_ID != en.C_ID).Count() > 0)
                return 0;
            else
            {
                var query = conn.T_CARDINFO.Select(c => c).Where(c => c.C_ID == id);
                foreach (var n in query)
                {
                    #region 绑定数据
                    n.C_I01 = en.C_I01;
                    n.C_STAUTS = en.C_STAUTS;
                    n.C_V01 = en.C_V01;
                    //n.C_V02 = en.C_V02;
                    //n.C_CBMP = en.C_CBMP;
                    //n.C_PIMG = en.C_PIMG;
                    //n.c_PDF = en.c_PDF;
                    #endregion
                }
                conn.SaveChanges();
                return 0;
            }
        }
        public static int Modify_CardV02(int id, T_CARDINFO en)
        {
            SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
            if (conn.T_CARDINFO.Select(c => c).Where(c => c.C_CARDNO == en.C_CARDNO && c.C_ID != en.C_ID).Count() > 0)
                return 0;
            else
            {
                var query = conn.T_CARDINFO.Select(c => c).Where(c => c.C_ID == id);
                foreach (var n in query)
                {
                    #region 绑定数据
                   // n.C_I01 = en.C_I01;
                   // n.C_STAUTS = en.C_STAUTS;
                    //n.C_V01 = en.C_V01;
                    n.C_V02 = en.C_V02;
                    //n.C_CBMP = en.C_CBMP;
                    //n.C_PIMG = en.C_PIMG;
                    //n.c_PDF = en.c_PDF;
                    #endregion
                }
                conn.SaveChanges();
                return 0;
            }
        }
        public static int Modify_CardPDF(int id, T_CARDINFO en)
        {
            SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
            if (conn.T_CARDINFO.Select(c => c).Where(c => c.C_CARDNO == en.C_CARDNO && c.C_ID != en.C_ID).Count() > 0)
                return 0;
            else
            {
                var query = conn.T_CARDINFO.Select(c => c).Where(c => c.C_ID == id);
                foreach (var n in query)
                {
                    #region 绑定数据
                    // n.C_I01 = en.C_I01;
                     n.C_STAUTS = en.C_STAUTS;
                    //n.C_V01 = en.C_V01;
                   // n.C_V02 = en.C_V02;
                    //n.C_CBMP = en.C_CBMP;
                    //n.C_PIMG = en.C_PIMG;
                    n.c_PDF = en.c_PDF;
                    #endregion
                }
                conn.SaveChanges();
                return 0;
            }
        }
        public static List<T_CARDINFO> Query_Cardno(string cardno , int stauts)
        {
            try
            {
                SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
                var query = conn.T_CARDINFO.Select(c => c);
                if (cardno!="" && cardno!=null)
                    query = query.Where(c => c.C_CARDNO == cardno);
                if (stauts != 0)
                    query = query.Where(c => c.C_STAUTS == stauts);
                return query.ToList();
            }
            catch { return null; }
        }
        /// <summary>
        /// 登陆用户查询未结束
        /// </summary>
        /// <param name="name"></param>
        /// <param name="status"></param>
        /// <returns></returns>
        public static List<T_CARDINFO> Query_TCarduname(string username)
        {
            try
            {
                SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
                var query = conn.T_CARDINFO.Select(c => c);
                if (username != "")
                    query = query.Where(c => c.C_UNAME==username&&c.C_I01!=2);
                return query.ToList();
            }
            catch { return null; }
        }

        /// <summary>
        /// 登陆用户查询未提交
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>
        public static List<T_CARDINFO> Query_TCarduname1(string username)
        {
            try
            {
                SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
                var query = conn.T_CARDINFO.Select(c => c);
                if (username != "")
                    query = query.Where(c => c.C_UNAME == username && c.C_STAUTS == 1);
                return query.ToList();
            }
            catch { return null; }
        }


        public static List<T_CARDINFO> Query_JSCX(string username)
        {
            try
            {
                SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
                var query = conn.T_CARDINFO.Select(c => c);
                if (username != "")
                    query = query.Where(c => c.C_UNAME == username && c.C_STAUTS == 2);
                   query = query.Where(c => c.C_I01 == 1|c.C_I01==-1);
                return query.ToList();
            }
            catch { return null; }
        }
        /// <summary>
        /// 根据ID查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static List<T_CARDINFO> Query_Cardid(int id)
        {
            try
            {
                SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
                var query = conn.T_CARDINFO.Select(c => c);
                if (id != 0)
                    query = query.Where(c => c.C_ID == id);
                return query.ToList();
            }
            catch { return null; }
        }
       
        /// <summary>
        /// 查询历史记录
        /// </summary>
        /// <param name="username"></param>
        /// <returns></returns>

        public static List<T_CARDINFO> Query_log(string username , DateTime start, DateTime end)
        {
            try
            {
                SgAutoArchiveBrrEntities conn = new SgAutoArchiveBrrEntities();
                var query = conn.T_CARDINFO.Select(c => c);
                if (username != "")
                    query = query.Where(c => c.C_UNAME == username && c.C_I01==2);
                if (start != null)
                    query = query.Where(c => c.C_DJDATE >= start && c.C_DJDATE <= end);
                return query.ToList();
                
            }
            catch { return null; }
        }
    }
}
